package com.dengyafei.service.impl;

import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.bean.PropagandaEntity;
import com.dengyafei.bean.PropagandaEntityExample;
import com.dengyafei.domin.dto.PropagandaSearchDTO;
import com.dengyafei.domin.vo.*;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.mapper.PropagandaEntityMapper;
import com.dengyafei.service.PropagandaService;
import com.dengyafei.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 邓亚非
 */
@Service
public class PropagandaServiceImpl implements PropagandaService {

    @Autowired
    PropagandaEntityMapper propagandaEntityMapper;
    @Autowired
    PropagandaEntityExample propagandaEntityExample;
    @Autowired
    HttpServletRequest request;
    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;
    /**
     * 根据hid查询对应的宣讲会
     * @return
     */
    @Override
    public List<PropagandaEntity> queryAllPropagandaByHid() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        PropagandaEntityExample.Criteria criteria = propagandaEntityExample.createCriteria().andHidEqualTo(enterpriseEntity.getHid());
        List<PropagandaEntity> propagandaEntities = propagandaEntityMapper.selectByExample(propagandaEntityExample);
        return propagandaEntities;
    }

    /**
     * 根据条件查询宣讲会
     * @param propagandaSearchVO
     * @return
     */
    @Override
    public List<PropagandaEntity> queryByParams(PropagandaSearchVO propagandaSearchVO) {
        PropagandaSearchDTO propagandaSearchDTO = BeanHelper.copyProperties(propagandaSearchVO, PropagandaSearchDTO.class);
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        List<PropagandaEntity> propagandaEntities = propagandaEntityMapper.queryByParams(enterpriseEntity.getHid(), propagandaSearchDTO.getSearchParams());
        return propagandaEntities;
    }

    /**
     * 添加一个宣讲会
     * @param propagandaAddVO
     * @return
     */
    @Override
    public Integer insertPropaganda(PropagandaAddVO propagandaAddVO) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        PropagandaEntity propagandaEntity = BeanHelper.copyProperties(propagandaAddVO, PropagandaEntity.class);
        propagandaEntity.setHid(enterpriseEntity.getHid());
        return propagandaEntityMapper.insertSelective(propagandaEntity);
    }

    /**
     * 查询单个宣讲会
     * @param queryVO
     * @return
     */
    @Override
    public PropagandaEntity queryByMid(PropagandaQueryVO queryVO) {
        return propagandaEntityMapper.selectByPrimaryKey(queryVO.getMid());
    }

    /**
     * 更新宣讲会
     * @param propagandaUpdateVO
     * @return
     */
    @Override
    public Integer update(PropagandaUpdateVO propagandaUpdateVO) {
        PropagandaEntity propagandaEntity = BeanHelper.copyProperties(propagandaUpdateVO, PropagandaEntity.class);
        return propagandaEntityMapper.updateByPrimaryKeySelective(propagandaEntity);
    }

    /**
     * 删除宣讲会
     * @param propagandaDeleteVO
     * @return
     */
    @Override
    public Integer delete(PropagandaDeleteVO propagandaDeleteVO) {
        return propagandaEntityMapper.deleteByPrimaryKey(propagandaDeleteVO.getMid());
    }

    /**
     * 查询所有的宣讲会
     * @return
     */
    @Override
    public List<PropagandaEntity> queryAll() {
        return propagandaEntityMapper.queryAll();
    }

    /**
     * 条件查询
     * @param propagandaSearchVO
     * @return
     */
    @Override
    public List<PropagandaEntity> search(PropagandaSearchVO propagandaSearchVO) {
        PropagandaSearchDTO propagandaSearchDTO = BeanHelper.copyProperties(propagandaSearchVO, PropagandaSearchDTO.class);
        return propagandaEntityMapper.queryPropagandas(propagandaSearchDTO);
    }
}